import WebSocketUtil from "@/util/WebSocketUtil";
import TRTCUtil from "@/util/TRTCUtil";
import {enterRoom, quitRoom} from "../../api/bureau/miniaccident/trtcroom";
import {getStore} from '@/util/store'

export default {
  state: {
    comingState: false, // false等待 true来电
    answeringState: false, // false拒接 true接听
    workState: 0,
    incomingMessage: { // cmd=1
      "callerId": "",
      "callerName": "",
      "roomId": "",
      "bizId": "",
      "privateMapKey": "",
      "accountType": "",
      "sdkAppID": "",
      "userSig": "",
      "address": "淮安市政府",
      "longitude": 119.112997,
      "latitude": 33.550945,
      "createTime": "2021-08-07 12:00:00"
    },
    tiAlarm: 0,
    missedAlarm: 0,
  },
  mutations: {
    setComingState(state, params) {
      state.comingState = params;
    },
    setAnsweringState(state, params) {
      state.answeringState = params;
    },
    setWorkState(state, params) {
      state.workState = params;
    },
    setIncomingMessage(state, params) {
      state.incomingMessage = params;
    },
    setTiAlarm(state, params) {
      state.tiAlarm = params
    },
    setMissedAlarm(state, params) {
      state.missedAlarm = params
    },
  },
  actions: {
    hangUpActions({state}) { // 挂断
      let that = this;
      let _userInfo = getStore({name: 'userInfo'});
      quitRoom({
        userId: _userInfo.userId,
        roomId: state.incomingMessage.roomId,
        bizId: state.incomingMessage.bizId,
        roomType: state.incomingMessage.roomType
      }).then(function () {
        that.dispatch("restState");
      });
    },
    answerAction({commit, state}) { // 接听
      let _userInfo = getStore({name: 'userInfo'});
      enterRoom({
        userId: _userInfo.userId,
        roomId: state.incomingMessage.roomId,
        bizId: state.incomingMessage.bizId
      }).then(function () {
        commit("setAnsweringState", true);
        commit("setComingState", false);
        let _roomType = state.incomingMessage.roomType;
        // 防止重复进入/pages/trtc-room/enter-room/enter-room
        if (_roomType == 0 || _roomType == 2 || _roomType == 3 || _roomType == 4) {
          console.log("----")
        } else {
          WebSocketUtil.sendWebSocket({
            cmd: 2,
            msg: state.incomingMessage.callerId,
            groupId: state.incomingMessage.roomId
          });
        }
      });
    },
    restState({commit}) {
      commit("setComingState", false);
      commit("setAnsweringState", false);
      commit("setIncomingMessage", {});
      commit("setWeather", "");
      commit("setAccidentTime", "");
      commit("setAccidentAddress", "");
      commit("setDescription", "");
      commit("setLaw", "");
      commit("setRule", "");
      commit("setPersonInfoList", [
        {
          index: "0",
          sort: "0",
          accidentPersonNum: "当事人1",
          name: "",
          idNumber: "",
          phoneNumber: "",
          drivingLicenseNumber: "",
          carNumber: "",
          sign: "nosign.png",
          carInfo: "",
          dutyId: ""
        }
      ]);
      commit("setImageInfoList", []);
      TRTCUtil.resetImageInfoList();
    }
  }
};
